package cn.z2huo.demo.spring.boot.objectprovider;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Component;

/**
 * <p>
 *
 * @author z2huo
 */
@RequiredArgsConstructor
@Component
class Test2 {

    private final ObjectProvider<ServiceA> serviceAProvider;

    /**
     * 相较于 {@link Test#serviceB}，这里使用 ObjectProvider 包裹 ServiceB，启动时不会报错
     */
    private final ObjectProvider<ServiceB> serviceBProvider;

    public void test() {
        ServiceA serviceA = serviceAProvider.getIfAvailable();
        ServiceB serviceB = serviceBProvider.getIfAvailable();
        if (serviceA != null) {
            serviceA.doSomething();
        }
        if (serviceB != null) {
            serviceB.doSomething();
        }
    }

}
